package com.ludomancy.sos64.components;

import java.awt.Graphics;
import java.awt.Point;
import java.util.Iterator;
import java.util.Vector;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.sos64.Component;
import com.ludomancy.sos64.Game;

public class Usine extends Component
{
	private ImageAnimation usine;
	private boolean active = false;
	
	public Usine(Point pos)
	{
		super(pos.x,pos.y);
		
		if(usine == null)
		{
			usine = new ImageAnimation(new ImageStrip(ResourceManager.getImage("usine.png"), 45, 37), 2);
		}
		usine.looping = true;
	}
	
	public boolean isMadeOfMetal()
	{
		return true;
	}
	
	public Point getSize()
	{
		return new Point(45,37);
	}
	
	public boolean canMove()
	{
		return false;
	}

	public void paint(Graphics g, int px, int py)
	{
		usine.paintCurrentFrame(g, px, py);
	}

	public void update()
	{
		usine.update();
		active = false;
		int power = 0;
		Vector turbines = Game.game.getComponents(Turbine.class);
		for (Iterator iterator = turbines.iterator(); iterator.hasNext();)
		{
			Turbine c = (Turbine) iterator.next();
			if(c.generating)
			{
				power++;
			}
		}
		
		if(power == 4 && Game.game.getComponents(Battery.class).isEmpty())
		{
			active = true;
			Game.game.add(new Battery(new Point(x + 12,y-4)));
		}
		
		if(active)
		{
			usine.play();
		}
		else
		{
			usine.rewind();
			usine.stop();
		}
	}
}
